package com.sgcc.pda.greendao;


import com.sgcc.pda.baselibrary.application.HZBaseApplication;
import com.sgcc.pda.baselibrary.log.HzLogSaveUtil;
import com.sgcc.pda.greendao.dao.DBOpenHelper;
import com.sgcc.pda.greendao.entity.DaoMaster;
import com.sgcc.pda.greendao.entity.DaoSession;

import org.greenrobot.greendao.database.Database;

/**
 * Created by cj on 2020/2/21.
 * 新写的greendao框架，MVC架构，简单好用。
 */
public class GreendaoManager {

    private static GreendaoManager instance = new GreendaoManager();
    private DaoSession daoSession;
    private Database db;

    private GreendaoManager() {
    }

    /**
     * 初始化建立数据库，可以在application启动时建表
     */
    public void init(String dbPath) {

        String fromPath = "/.platform/data/lib/libsqlcipher.so";
        System.load(fromPath);

        DaoMaster.OpenHelper helper = new DBOpenHelper(HZBaseApplication.getInstance(), dbPath);
        helper.setLoadSQLCipherNativeLibs(false);
//        Database db = helper.getWritableDb();
//        if (db != null) db.close();
        db = helper.getEncryptedWritableDb("hzwq.888");
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        HzLogSaveUtil.saveLog("daoSession初始化成功，"+daoSession.toString(), HzLogSaveUtil.LogType.DATABASE);
    }


    public static GreendaoManager getInstance() {
//        if (instance == null) {
//            synchronized (GreendaoManager.class) {
//                if (instance == null) {
//                    instance = new GreendaoManager();
//                }
//            }
//        }
        return instance;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }


}
